<?php
/*define('IN_ADMINCP', TRUE);
define('IN_TESTCP', TRUE);
define('TESTCP_AJAX', AJAX);
define('TESTCP_PATH', dirname(__FILE__));
define('APP_PATH', dirname(TESTCP_PATH));
//echo TESTCP_PATH . '--' . APP_PATH;die;
require APP_PATH . '/app.php';
$admincp = new admincp;
$account = new admincp_account;*/
//上面的代码采用杨坤的框架，下面的代码采用原生PHP
include "center/Model.class.php";
include "center/Route.class.php";
class Add extends Model{
    public $error;
    public $admin='小编';
    public function __construct(){

    }
    public function AddDownload(){
        //判断ajax请求
        if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
            $this->Ajax();
        }
        //获取第一级的分类
        $level1="SELECT * FROM oa_download_category WHERE level=1";
        $level1Data=parent::all($level1);
        if(isset($_POST['submit'])){
            $data=array(
                'admin'=>"'".$this->admin."'",
                'name'=>"'".$_POST['name']."'",
                'url'=>"'".$_POST['url']."'",
                'password'=>"'".$_POST['password']."'",
                'level1'=>"'".$_POST['level1']."'",
                'level2'=>"'".$_POST['level2']."'",
                'level3'=>"'".$_POST['level3']."'",
                'time'=>time(),
            );
            //var_dump($data);exit;
            if(empty($_POST['name']) || empty($_POST['url'])){
                $this->error='必填选项不得为空！';
            }else{
                $sql="INSERT INTO oa_download(admin,name,url,password,level1,level2,level3,create_tim)
		            VALUES ({$data['admin']},
                            {$data['name']},
                            {$data['url']},
                            {$data['password']},
                            {$data['level1']},
                            {$data['level2']},
                            {$data['level3']},
                            {$data['time']}
					)";
                $a=parent::aud($sql);
                if($a==1){
                    echo '<script>alert("文档添加成功！");</script>';
                }else{
                    echo '<script>alert("文档添加失败！请检查文档！");</script>';
                }
                //Header("Location:".Route::Url('index'));
            }
        }
        return array(
            'info'=>'添加文档时注意必填信息！',
            'title'=>'添加文档',
            'level1Data'=>$level1Data,
            'data'=>$data,
        );
    }
    private function Ajax(){
        $cid=$_POST['cid'];
        $level="SELECT * FROM oa_download_category WHERE parent_cid=$cid";
        echo json_encode(parent::all($level));
        exit;
    }
}
$addModel=new Add();
$data=$addModel->AddDownload();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <link rel="stylesheet" type="text/css" href="assets/css/editor.css"/>
    <link rel="stylesheet" type="text/css" href="assets/css/public.css"/>
    <script type="text/javascript" src="assets/js/jquery-1.9.1.min.js"></script>
</head>
<body>
<div id="content">
    <div id="content-box">
        <div class="content-head">
            <div class="keyboard padding-l40">
                <input type="text" class="search padding-l40 color-black" placeholder="站内搜索"/>
                <div class="keyboard-botton">
                    <a href="javascript:void(0);"><img src="assets/backend-img/content-head/settings.png"/></a>
                    <a href="javascript:void(0);"><img src="assets/backend-img/content-head/language.png"/></a>
                    <a href="javascript:void(0);" class="information"><img src="assets/backend-img/content-head/information.png"/></a>
                    <a href="javascript:void(0);" class="on-off"><img src="assets/backend-img/content-head/on-off.png"/></a>
                </div>
            </div>
            <div class="navigation padding-l40">
                <a href="javascript:void(0);">导航管理</a><a href="javascript:void(0);" class="rating"><?= $data['title'];?></a><span><?= $data['info'];?></span>
            </div>
        </div>
        <div class="content">
            <div class="box">
                <form action="" method="post">
                    <div style="color:red;">
                        <?php if($addModel->error){ echo $addModel->error;}?>
                    </div>
                    <div class="input width60">
                        <label>文档标题：</label>
                        <input type="text" name="name" class="input-text400" placeholder="请输入文章标题" value=""/>
                        <span style="color:red;">*(必填)</span>
                    </div>
                    <div class="state">
                        <div class="float-l">
                            业务方向：
                        </div>
                        <input type="hidden" name='level1' value=""/>
                        <?php foreach($data['level1Data'] as $k=>$v):?>
                            <p class="state-radio state-off" data-cid="<?= $v['cid']; ?>"></p>
                            <p class="lable-p"><?= $v['name'];?></p>
                        <?php endforeach;?>
                    </div>
                    <div class="state" id="level2" style="display: none;">
                    </div>
                    <div class="state" id="level3" style="display: none;">

                    </div>
                    <div class="input width60">
                        <label>下载链接：</label>
                        <input type="text" name="url" class="input-text400" placeholder="请输入文章标题"/>
                        <span style="color:red;">*(必填)</span>
                    </div>
                    <div class="input width60">
                        <label>下载密码：</label>
                        <input type="text" name="password" class="input-text150" placeholder="请输入文章标题"/>
                    </div>
                    <div class=" clear width1024">
                        <a href="javascript:void(0);" class="undo">取消并返回</a>
                        <input type="submit" name="submit" class="submit" value="确认"/>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<?php include "menu.php"; ?>
<script>
    //单选框点击样式,这里目前使用面向对象的写法，虽然简单但是过程不好理解，以后将使用foreach遍历的方法
    (function($){
        //这里使用了实践冒泡，事件代理
        $('#content-box').on('click','.state-radio', function(){
            //改变同级别元素的样式
            $(this).siblings('.state-radio').removeClass('state-on state-off');
            $(this).siblings('.state-radio').addClass('state-off');
            //改变自身的样式
            $(this).removeClass('state-off');
            $(this).addClass('state-on');
            //获取当前分类的id，并设置input的值
            var cid=$(this).data('cid');
            $(this).siblings('input').val(cid);
            //判断是否已经到达第三级目录，如果到达第三级目录就不需要请求数据
            if($(this).siblings('input').attr('name')=='level3'){
                return;
            }
            $.ajax({
                type:'post',
                url:'<?= Route::Url('edit'); ?>',
                data:{cid:cid},
                success:function(response,status,xhr){
                    //debugger;
                    var ajaxs=JSON.parse(response);
                    if(ajaxs[0]!==undefined && ajaxs[0]['level']==2){
                        var htmls=createHtml("文件类型：",ajaxs,2);
                        $('#level2').css('display','block');
                        $('#level2').html(htmls);
                        $('#level3').css('display','none');
                        $('#level3').html('');
                    }
                    if(ajaxs[0]!==undefined && ajaxs[0]['level']==3){
                        var htmls=createHtml("产品分类：",ajaxs,3);
                        $('#level3').css('display','block');
                        $('#level3').html(htmls);
                    }else{
                        $('#level3').css('display','none');
                        $('#level3').html('');
                    }

                    console.log(ajaxs);
                }
            });
            function createHtml(info,ajaxs,level){
                var htmls='';
                htmls+="<div class='float-l'>"+info+"</div>";
                htmls+="<input type='hidden' name='level"+level+"' value='' />";
                for(var i=0;i<ajaxs.length;i++){
                    htmls+="<p class='state-radio state-off' data-cid='"+ajaxs[i]['cid']+"'></p>";
                    htmls+="<p class='lable-p'>"+ajaxs[i]['name']+"</p>";
                }
                return htmls;
            }
        });
    })(jQuery)
</script>
</body>
</html>